package com.wowudi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wowudi.entity.Equipment;
import com.wowudi.entity.EquipmentRecords;
import com.wowudi.mapper.EquipmentMapper;
import com.wowudi.mapper.EquipmentRecordsMapper;
import com.wowudi.service.EquipmentRecordsService;
import com.wowudi.service.EquipmentService;
import com.wowudi.service.impl.EquipmentRecordsServiceImpl;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("api/dept/equips")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private EquipmentMapper equipmentMapper;


    @Autowired
    private EquipmentRecordsMapper equipmentRecordsMapper;
    @Autowired
    private EquipmentRecordsServiceImpl equipmentRecordsService;


    @RequestMapping("equip")
    public Page<Equipment> selectoutdep(int pages, int sizes) {
        Page<Equipment> productIPage=new Page<>(pages, sizes);
        productIPage=equipmentService.page(productIPage);
        return productIPage;
    }
    @RequestMapping("eqdep")
    public IPage<Equipment> selectdpcdep(int pages, int sizes, String eqName) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("eq_name", eqName);
        Page<Equipment> productIPage=new Page<>(pages, sizes);
        IPage<Equipment> pageList = equipmentMapper.selectPage(productIPage, queryWrapper);
        return pageList;
    }

    @Options(useGeneratedKeys = true,keyColumn = "eq_id",keyProperty = "eqId")
    @RequestMapping("add")
    public int  adddeq(int dpc_id, String eq_name, BigDecimal eq_price, int eq_number, String eq_saplings, String eq_state) {
        Equipment equipment = new Equipment();
        equipment.setDpcId(dpc_id);
        equipment.setEqName(eq_name);
        equipment.setEqPrice(eq_price);
        equipment.setEqNumber(eq_number);
        equipment.setEqSaplings(eq_saplings);
        equipment.setEqState(eq_state);
        equipmentMapper.insert(equipment);
        EquipmentRecords equipmentRecords = new EquipmentRecords();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Date now = null;
        try {
            now = sdf.parse(sdf.format(new Date()));
            equipmentRecords.setEqId(equipment.getEqId());
            equipmentRecords.setEqPrice(eq_price);
            equipmentRecords.setEqNumber(eq_number);
            equipmentRecords.setEqPurchaseTime(now);
            equipmentRecordsService.save(equipmentRecords);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
